package com.yonyou.gmmc.service.wechatpush.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.marketing.common.Response;
import org.marketing.common.constants.ResultMsgConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.yonyou.gmmc.service.wechatpush.common.ApiVersionConsts;
import com.yonyou.gmmc.service.wechatpush.dto.AlterReplyDto;
import com.yonyou.gmmc.service.wechatpush.dto.KeyWordReply;
import com.yonyou.gmmc.service.wechatpush.dto.NewsActivityDto;
import com.yonyou.gmmc.service.wechatpush.dto.WechatSmartReplyDto;
import com.yonyou.gmmc.service.wechatpush.entity.TmCarKnowledges;
import com.yonyou.gmmc.service.wechatpush.model.TtUserProblem;
import com.yonyou.gmmc.service.wechatpush.service.CarKnowledgeInterface;
import com.yonyou.gmmc.service.wechatpush.service.WechatSmartReplyService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = ApiVersionConsts.BASE_PATH+ApiVersionConsts.VERSION+"/smartReply" )
@Api("智能回复相关API")
public class WechatSmartReplyController {
	
	private static final Logger logger = Logger.getLogger(WechatSmartReplyController.class);
	
	@Autowired
	private  WechatSmartReplyService wechatSmartReplyService;
	@Autowired
	private CarKnowledgeInterface carKnowledgeInterface;
	
	@RequestMapping(value = "/ifExistRule", method = RequestMethod.GET,produces = "application/json;UTF-8")
	@ApiOperation(value = "判断规则", notes = "判断规则")
	public Response<Map<String, Object>> ifExistRule(@RequestParam("ruleName") String ruleName) {
		Map<String,Object> result = new HashMap<String,Object>();
		try {
			result = wechatSmartReplyService.ifExistRule(ruleName);
            return new Response<Map<String, Object>>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new Response<Map<String, Object>>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
	}
	
	@RequestMapping(value = "/insertReply", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "插入智能回复", notes = "插入智能回复")
	public Response<Map<String, Object>>  insertSmartReply(@RequestBody @Valid KeyWordReply keyWordReply,
			HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<>();
		try {
			resultMap = wechatSmartReplyService.insertSmartReply(keyWordReply.getReplyDto(),keyWordReply.getKeywordDto());
            return new Response<Map<String, Object>>(ResultMsgConstants.RESULT_CODE_SUCCESS, resultMap, "");
        } catch (Exception e) {
            logger.error("插入发生异常", e);
            return new Response<Map<String, Object>>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
	}
	
	@RequestMapping(value = "/queryReplyRule", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "智能回复规则查询", notes = "智能回复规则查询")
	public Response<PageInfo<WechatSmartReplyDto>> getReplyRule(HttpServletRequest request,
			@RequestParam(required=false,defaultValue="1") Integer pageNum
     		,@RequestParam(required=false,defaultValue="rule_name") String sort
     		,@RequestParam(required=false,defaultValue="desc") String sortType
     		,@RequestParam(required=false,defaultValue="10") String pageSize
     		,@RequestParam(required=false,defaultValue="") String ruleName
     		,@RequestParam(required=false,defaultValue="") String keyWord
     		,@RequestParam(required=false,defaultValue="") String status) {
		try {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("pageNum", pageNum);
	    	paramMap.put("pageSize", pageSize);
	    	paramMap.put("sort", sort);
	    	paramMap.put("sortType", sortType);
	    	paramMap.put("ruleName", ruleName);
	    	paramMap.put("keyWord", keyWord);
	    	paramMap.put("status", status);
	    	logger.info(status);
	    	PageInfo<WechatSmartReplyDto> retMap = wechatSmartReplyService.getReplyRule(paramMap);
			return new Response<PageInfo<WechatSmartReplyDto>>(ResultMsgConstants.RESULT_CODE_SUCCESS, retMap, "");
        } catch (Exception e) {
            logger.error("查询错误", e);
            return new Response<PageInfo<WechatSmartReplyDto>>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
	}
	
	@RequestMapping(value = "/queryReplyDetail", method = RequestMethod.GET,produces = "application/json;UTF-8")
	@ApiOperation(value = "查询回复详情", notes = "查询回复详情")
	public Response<Map<String,Object>> queryReplyDetail(@RequestParam("rootId") Integer rootId) {
		Map<String,Object> result = new HashMap<String,Object>();
		try {
			result = wechatSmartReplyService.getMenuReplyDetail(rootId);
            return new Response<Map<String,Object>>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new Response<Map<String,Object>>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
	}
	
	@RequestMapping(value = "/updateStatus", method = RequestMethod.GET,produces = "application/json;UTF-8")
	@ApiOperation(value = "更新状态", notes = "更新状态")
	public Response<Map<String,Object>> updateStatus(@RequestParam("rootId") Integer rootId,@RequestParam("status") Integer status) {
		Map<String,Object> result = new HashMap<String,Object>();
		try {
			result = wechatSmartReplyService.updateStatus(rootId, status);
            return new Response<Map<String,Object>>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
        } catch (Exception e) {
            logger.error("更新发生发生异常", e);
            return new Response<Map<String,Object>>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
	}
	
	@RequestMapping(value = "/queryInfor", method = RequestMethod.GET,produces = "application/json;UTF-8")
	@ApiOperation(value = "查询资讯", notes = "查询资讯")
	public Response<PageInfo<NewsActivityDto>> queryInfor(@RequestParam(required=false,defaultValue="1") Integer pageNum,
     							@RequestParam(required=false,defaultValue="img") String sort,
     							@RequestParam(required=false,defaultValue="desc") String sortType,
     							@RequestParam(required=false,defaultValue="10") String pageSize,
     							@RequestParam(required=false,defaultValue="") String title,
 								@RequestParam(required=false,defaultValue="") Integer newsType,
 								@RequestParam(required=false,defaultValue="") String startTime,
 								@RequestParam(required=false,defaultValue="") String endTime,
 								@RequestParam(required=false,defaultValue="") Integer status,
 								@RequestParam(required=false,defaultValue="") String dealerCode,
 								@RequestParam(required=false,defaultValue="") String publishStartDate,
 								@RequestParam(required=false,defaultValue="") String publishEndDate) {
		try {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("pageNum", pageNum);
	    	paramMap.put("pageSize", pageSize);
	    	paramMap.put("sort", sort);
	    	paramMap.put("sortType", sortType);
	    	paramMap.put("title", title);
	    	paramMap.put("newsType", newsType);
	    	paramMap.put("startTime", startTime);
	    	paramMap.put("endTime", endTime);
	    	paramMap.put("status", status);
	    	paramMap.put("dealerCode", dealerCode);
	    	paramMap.put("publishStartDate", publishStartDate);
	    	paramMap.put("publishEndDate", publishEndDate);
	    			
	    	PageInfo<NewsActivityDto> retMap = wechatSmartReplyService.queryInforOrActity(0,paramMap);
            return new Response<PageInfo<NewsActivityDto>>(ResultMsgConstants.RESULT_CODE_SUCCESS, retMap, "");
        } catch (Exception e) {
            logger.error("查询错误", e);
            return new Response<PageInfo<NewsActivityDto>>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
	}
	
	@RequestMapping(value = "/queryActivity", method = RequestMethod.GET,produces = "application/json;UTF-8")
	@ApiOperation(value = "查询活动", notes = "查询活动")
	public Response<PageInfo<NewsActivityDto>> queryActivity(@RequestParam(required=false,defaultValue="1") Integer pageNum,
     								@RequestParam(required=false,defaultValue="img") String sort,
     								@RequestParam(required=false,defaultValue="desc") String sortType,
     								@RequestParam(required=false,defaultValue="10") String pageSize,
     								
     								@RequestParam(required=false,defaultValue="") String title,
     								@RequestParam(required=false,defaultValue="") String startTime,
     								@RequestParam(required=false,defaultValue="") String endTime,
     								@RequestParam(required=false,defaultValue="") String status,
     								@RequestParam(required=false,defaultValue="") String dealerCode,
     								@RequestParam(required=false,defaultValue="") String publishStartDate,
     								@RequestParam(required=false,defaultValue="") String publishEndDate) {
		try {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("pageNum", pageNum);
	    	paramMap.put("pageSize", pageSize);
	    	paramMap.put("sort", sort);
	    	paramMap.put("sortType", sortType);
	    	paramMap.put("title", title);
	    	paramMap.put("startTime", startTime);
	    	paramMap.put("endTime", endTime);
	    	paramMap.put("status", status);
	    	paramMap.put("dealerCode", dealerCode);
	    	paramMap.put("publishStartDate", publishStartDate);
	    	paramMap.put("publishEndDate", publishEndDate);
	    	PageInfo<NewsActivityDto> retMap = wechatSmartReplyService.queryInforOrActity(1,paramMap);
            return new Response<PageInfo<NewsActivityDto>>(ResultMsgConstants.RESULT_CODE_SUCCESS, retMap, "");
        } catch (Exception e) {
            logger.error("查询错误", e);
            return new Response<PageInfo<NewsActivityDto>>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
	}
	
	@RequestMapping(value = "/queryCarKnowledge", method = RequestMethod.GET,produces = "application/json;UTF-8")
	@ApiOperation(value = "查询用车知识", notes = "查询用车知识")
	public Response<PageInfo<TmCarKnowledges>> queryCarKnowledge(@RequestParam(required=false,defaultValue="0") Integer pageNum
    		,@RequestParam(required=false,defaultValue="10") String pageSize
    		,@RequestParam(required=false,defaultValue="") String title) {
		Response<PageInfo<TmCarKnowledges>> result;
		try {
			result = carKnowledgeInterface.queryCarKnowledge(pageNum, pageSize, title);
            return result;
        } catch (Exception e) {
            logger.error("查询错误", e);
            return new Response<PageInfo<TmCarKnowledges>>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
	}
	
	@RequestMapping(value = "/insertSubMenuChild", method = RequestMethod.POST,produces = "application/json;UTF-8")
	@ApiOperation(value = "插入菜单的下级菜单或回复", notes = "插入菜单的下级菜单或回复")
	public Response<Map<String,Object>> insertSubMenuChild(@RequestBody @Valid List<WechatSmartReplyDto> listDto) {
		Map<String,Object> result = new HashMap<>(); 
		try {
			result = wechatSmartReplyService.insertSubMenuChild(listDto);
			return new Response<Map<String,Object>>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
        } catch (Exception e) {
            logger.error("插入错误", e);
            return new Response<Map<String,Object>>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
	}
	
	@RequestMapping(value = "/queryLeavesDetail", method = RequestMethod.GET,produces = "application/json;UTF-8")
	@ApiOperation(value = "查询文字图文详情", notes = "查询文字图文详情")
	public Response<Map<String,Object>> queryLeavesDetail(@RequestParam("replyId") Integer replyId) {
		Map<String,Object> result = new HashMap<String,Object>();
		try {
			result = wechatSmartReplyService.queryReply(replyId);
            return new Response<Map<String,Object>>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new Response<Map<String,Object>>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
	}
	
	@RequestMapping(value = "/updatekeyWordReply", method = RequestMethod.POST,produces = "application/json;UTF-8")
	@ApiOperation(value = "修改智能回复", notes = "修改智能回复")
	public Response<Map<String,Object>> updatekeyWordReply(@RequestBody @Valid KeyWordReply keyWordReply,
			HttpServletRequest request) {
		Map<String,Object> result = new HashMap<String,Object>();
		KeyWordReply keyWordReply2 = new KeyWordReply();
		keyWordReply2.setKeywordDto(keyWordReply.getKeywordDto());
		keyWordReply2.setReplyDto(keyWordReply.getReplyDto());
		try {
			result = wechatSmartReplyService.updatekeyWordReply(keyWordReply2.getReplyDto(),keyWordReply2.getKeywordDto());
            return new Response<Map<String,Object>>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
        } catch (Exception e) {
            logger.error("更新异常", e);
            return new Response<Map<String,Object>>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
	}
	
	@RequestMapping(value = "/queryNewsPublicer", method = RequestMethod.GET,produces = "application/json;UTF-8")
	@ApiOperation(value = "查询资讯发布方", notes = "查询资讯发布方")
	public Response<Map<String,Object>> queryNewsPublicer() {
		Map<String,Object> result = new HashMap<String,Object>();
		try {
			result = wechatSmartReplyService.queryNewsPublicer();
            return new Response<Map<String,Object>>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new Response<Map<String,Object>>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
	}
	
	@RequestMapping(value = "/deleteReply", method = RequestMethod.POST,produces = "application/json;UTF-8")
	@ApiOperation(value = "删除子菜单节点或回复", notes = "删除子菜单节点或回复")
	public Response<Map<String,Object>> deleteReply(@RequestParam("replyId") Integer replyId) {
		Map<String,Object> result = new HashMap<String,Object>();
		try {
			result = wechatSmartReplyService.deleteReply(replyId);
            return new Response<Map<String,Object>>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
        } catch (Exception e) {
            logger.error("删除发生异常", e);
            return new Response<Map<String,Object>>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
	}
	
	@RequestMapping(value = "/alters", method = RequestMethod.POST,produces = "application/json;UTF-8")
	@ApiOperation(value = "编辑", notes = "编辑")
	@ResponseBody
	public Response<Map<String,Object>> alterReply(@RequestBody AlterReplyDto alterReplyDto) {
		Map<String,Object> result = new HashMap<String,Object>();
		try {
			//代码扫描  1497
			AlterReplyDto alterReplyDto2 = new AlterReplyDto();
			alterReplyDto2.setKeyWordReply(alterReplyDto.getKeyWordReply());
			alterReplyDto2.setMenuList(alterReplyDto.getMenuList());
			alterReplyDto2.setReplyId(alterReplyDto.getReplyId());
			alterReplyDto2.setStatus(alterReplyDto.getStatus());
			result = wechatSmartReplyService.alterReply(alterReplyDto2);
            return new Response<Map<String,Object>>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
        } catch (Exception e) {
            logger.error("修改发生异常", e);
            return new Response<Map<String,Object>>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
	}

	@RequestMapping(value = "/test", method = RequestMethod.POST,produces = "application/json;UTF-8")
    public Response<Map<String,Object>> test(){
	    Map<String,Object> result = new HashMap<String,Object>();
        try {
            result = wechatSmartReplyService.test();
            return new Response<Map<String,Object>>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
        } catch (Exception e) {
            logger.error("修改发生异常", e);
            return new Response<Map<String,Object>>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
	}
	
	   @RequestMapping(value = "/count", method = RequestMethod.GET,produces = "application/json;UTF-8")
	   @ApiOperation(value = "智能回复提问统计", notes = "智能回复提问统计")
	   public Response<PageInfo<TtUserProblem>> queryCount(@RequestParam(required=false,defaultValue="1") Integer pageNum
	                                                    ,@RequestParam(required=false,defaultValue="10") String pageSize
	                                                    ,@RequestParam(required=false,defaultValue="") String content){
	       try{
	           PageInfo<TtUserProblem> list =wechatSmartReplyService.queryCount(pageSize,pageNum,content);
	           return new Response<PageInfo<TtUserProblem>>(ResultMsgConstants.RESULT_CODE_SUCCESS, list, "");
	       } catch (Exception e) {
	            logger.error("查询发生异常", e);
	            return new Response<PageInfo<TtUserProblem>>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
	        }
	   }
}
